<?php


namespace App\Http\Controllers\Customer\Product;


use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Product\Import\ImportRequest;
use App\Http\Searchs\Customer\Product\ImportSearch;
use App\Repositories\ProductImport;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class ImportController extends Controller
{
    /**
     * @var ProductImport
     */
    private $import;

    /**
     * ImportController constructor.
     * @param ProductImport $import
     */
    public function __construct(ProductImport $import)
    {
        $this->import = $import;
    }

    /**
     * @param ImportSearch $search
     * @return JsonResponse
     */
    public function index(ImportSearch $search)
    {
        return $this->response(
            $this->import->paginate($search->toArray())
        );
    }

    /**
     * @param ImportRequest $request
     * @return JsonResponse
     */
    public function store(ImportRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->import->create($request->only(['customer_id', 'file', 'type', 'remark']));
            })
        );
    }
}
